package models

import "github.com/jinzhu/gorm"

type Article struct {
	Id         int    `gorm:"primary_key" json:"id"`
	TagId      int    `json:"tag_id"`
	Name       string `json:"name"`
	Content    string `json:"content"`
	ModifiedBy string `json:"modified_by"`
	CreatedBy  string `json:"created_by"`
	Time       string `json:"time"`
	Thumbs     int    `json:"thumbs"`
}

func SelectArticle(maps interface{}) (articles []Article) {
	db.Where(maps).First(&articles)
	return articles
}

func SelectArticleBasic(maps interface{}) (articles []Article) {
	db.Where(maps).Select("id,tag_id,name,time").Find(&articles)
	return articles
}

func InsertArticle(maps map[string]interface{}) error {
	article := Article{
		TagId:     maps["TagId"].(int),
		Name:      maps["Name"].(string),
		Content:   maps["Content"].(string),
		CreatedBy: maps["CreatedBy"].(string),
		Time:      maps["Time"].(string),
		Thumbs:    maps["Thumbs"].(int),
	}

	if err := db.Model(&Article{}).Create(&article).Error; err != nil {
		return err
	}
	return nil
}

func DelArticle(maps interface{}) error {
	if err := db.Where(maps).Delete(&Article{}).Error; err != nil {
		return err
	}
	return nil
}

func AddThumbs(maps interface{}) error {
	if err := db.Model(&Article{}).Where(maps).Update("thumbs", gorm.Expr("thumbs + ?", 1)).Error; err != nil {
		return err
	}
	return nil
}
